import subprocess
import sys,os,time
from PyQt5.QtWidgets import QApplication,QWidget, QVBoxLayout,QHBoxLayout,QPushButton
from PyQt5.QtWidgets import QListWidget,QMenu, QAction, QFileDialog,QComboBox
from PyQt5.QtCore import Qt,QPoint,QRect
from PyQt5.QtGui import QPainter, QColor
from PyQt5.QtCore import Qt
import execute
import keyboard

class FileListWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('File List')
        layout = QVBoxLayout()
        layout.setContentsMargins(0,0,0,0)
        # 创建 QListWidget
        self.list_widget = QListWidget()
        # 启用拖放
        self.list_widget.setDragDropMode(QListWidget.InternalMove)
        # 将 QListWidget 添加到布局中
        layout.addWidget(self.list_widget)
        self.setLayout(layout)
        # 添加右键菜单
        self.list_widget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.list_widget.customContextMenuRequested.connect(self.show_context_menu)
        # 创建菜单项
        self.insert_action = QAction("添加文件", self)
        self.insert_action.triggered.connect(self.insert_file)
        self.delete_action = QAction("删除", self)
        self.delete_action.triggered.connect(self.delete_selected_row)
        self.clear_action = QAction("清空", self)
        self.clear_action.triggered.connect(self.clear_all_row)

    def add_files(self, file_paths):        # 将文件路径添加到 QListWidget 中
        for file_path in file_paths:
            self.list_widget.addItem(file_path)

    def show_context_menu(self, pos):   # 显示右键菜单
        menu = QMenu(self)
        menu.addAction(self.insert_action)
        menu.addAction(self.delete_action)
        menu.addAction(self.clear_action)
        menu.exec_(self.list_widget.mapToGlobal(pos))

    def delete_selected_row(self):  # 删除选中的行
        for item in self.list_widget.selectedItems():
            self.list_widget.takeItem(self.list_widget.row(item))

    def clear_all_row(self):  # 删除所有
        self.list_widget.clear()

    def insert_file(self):    # 打开文件选择对话框并插入选中的文件
        file_paths, _ = QFileDialog.getOpenFileNames(self, "选择文件", "", "All Files (*)")
        if file_paths:
            for file_path in file_paths:
                self.list_widget.addItem(file_path)

    def get_file_names(self):   # 获取所有文件名，并按列表顺序排列
        file_names = [self.list_widget.item(i).text() for i in range(self.list_widget.count())]
        return file_names

class MainWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("自动测试小公举")
        self.resize(300,200)

        # -----------------------------------------
        self.recordButton = QPushButton("录制")
        self.runButton = QPushButton("执行")
        self.fileWidget = FileListWidget()

        hLayout_2 = QHBoxLayout()
        hLayout_2.setContentsMargins(0,0,0,0)

        self.speed_combobox = QComboBox()
        speeds = ['3.0', '2.0', '1.75', '1.5', '1.25', '1.0', '0.75', '0.5', '0.2']
        self.speed_combobox.addItems(speeds)
        self.speed_combobox.setCurrentIndex(5) 

        hLayout_2.addStretch()
        hLayout_2.addWidget(self.recordButton)
        hLayout_2.addWidget(self.speed_combobox)
        hLayout_2.addWidget(self.runButton)

        # -----------------------------------------
        vMainLayout = QVBoxLayout()
        vMainLayout.setContentsMargins(5,5,5,5)
        vMainLayout.setSpacing(5)
        vMainLayout.addLayout(hLayout_2)
        vMainLayout.addWidget(self.fileWidget,1)
        self.setLayout(vMainLayout)

        # -----------------------------------------
        self.recordButton.clicked.connect(self.record)
        self.runButton.clicked.connect(self.exec)

    def record(self):
        current_directory = os.path.dirname(os.path.abspath(__file__))
        script_path = os.path.join(current_directory, 'record.py')
        subprocess.run(['python', script_path]) # 执行Python脚本

    def exec(self):
        files = self.fileWidget.get_file_names()
        execute.speed = float(self.speed_combobox.currentText())
        execute.quit = False
        for fileName in files :
            execute.runRecordFile(fileName)
            time.sleep(1)

    # 设置事件回调函数
    def quitExec():
        execute.quit = True


if __name__ == '__main__':
    app = QApplication(sys.argv)

    auTestWidget = MainWidget()
    auTestWidget.show()

    # keyboard.add_hotkey('+'.join(["ctrl", "shift", "q"]), auTestWidget.quitExec)

    sys.exit(app.exec_())

